<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <script>
        var z = 30;
        function fn1() {
            var x = 10;
            function fn2() {
                var y = 20;
                console.log(y);
                console.log(x);
                console.log(z);
            }
            fn2();
            // fn2();
            // console.log(x);
            // console.log(y); // ReferenceError: y is not defined
        }
        fn1();
        //　匿名函数
         var f = function() {
            console.log("hello world");
        }
        f();
        // 立即执行函数
        (function() {
            var a = 10;
            console.log(a);
        })();
        // 闭包
        function createCounter() {
            var count = 0;
            return function() {
                return count++;
            }
        var counter1 = createCounter();
        var counter2 = createCounter();
        console.log(counter1()); // 0
        console.log(counter1()); // 1
        console.log(counter2()); // 0
        console.log(counter2()); // 1
        }
        createCounter();
        // 块级作用域
        {
            let a = 10;
            console.log(a);
        }

        // 箭头函数
        var add = (a, b) => a + b;
        console.log(add(10, 20)); // 30
    </script>
</body>
</html>